﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WanPiJie.Utilities
{
    /// <summary>
    /// 页面基类
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.RegisterHttpMethodProcess();
        }
        protected void RegisterHttpMethodProcess()
        {
            switch (Request.HttpMethod)
            {
                case "GET":
                    this.HttpGetProcess();
                    break;
                case "POST":
                    this.HttpPostProcess();
                    break;
                case "PUT":
                    this.HttpPutProcess();
                    break;
                case "DELETE":
                    this.HttpDeleteProcess();
                    break;
                case "HEAD":
                    this.HttpHeadProcess();
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void HttpGetProcess() { }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void HttpPostProcess() { }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void HttpPutProcess() { }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void HttpDeleteProcess() { }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void HttpHeadProcess() { }

        #region By葛伟

        /// <summary>
        /// PV:Post Value 获取Post访问方式，表单控件的值
        /// </summary>
        /// <param name="controlName">表单控件的名称</param>
        /// <returns>表单控件的值</returns>
        public String PV(String controlName)
        {
            return this.Request.Form[controlName];
        }

        /// <summary>
        /// QV:Query Value:获取Get访问时的查询参数;
        /// </summary>
        /// <param name="queryString">查询变量的名称</param>
        /// <returns></returns>
        public String QV(String queryString)
        {
            return this.Request.QueryString[queryString];
        }

        /// <summary>
        /// 弹出警告消息
        /// </summary>
        /// <param name="message">消息</param>
        public virtual void ShowMessage(string message)
        {
            WriteScript("alert('" + message + "')");
        }

        /// <summary>
        /// 提示后跳转
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <param name="location">跳转的url</param>
        public virtual void ShowMessage(String message, String location)
        {
            ShowMessage(message);
            WriteScript("window.location=" + location);
        }

        /// <summary>
        /// 向客户端添加脚本
        /// </summary>
        /// <param name="code">脚本内容</param>
        public void WriteScript(String code)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(),
            Guid.NewGuid().ToString(), ";" + code, true);
        }
        #endregion
    }

    /// <summary>
    /// 页面基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageBase<T> : PageBase
    {
        /// <summary>
        /// 模型对象
        /// </summary>
        public T Model { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Model = Activator.CreateInstance<T>();
        }
    }
}